// $Id: user_events.js 289 2008-09-29 15:29:06Z hugo.wetterberg $

var UserEvents = function(block) {
  var keyed_feeds = function(feed_data) {
      var feeds = {},
          feed_count = feed_data.length, 
          prefix = 'city_group/group_events/';
      for (var path in feed_data) {
        if(path.substring(0, prefix.length) == prefix)
        {
          feeds[path] = feed_data[path][1];
        }
      }
      return feeds;
    };
  var leadingZeroes = function(number, numberOfZeroes)
  {
    if(!numberOfZeroes)
    {
      numberOfZeroes = 2;
    }
  },
    months = [0, 'Jan', 'Feb', 'Mar', 'Apr', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    weekdays = [Drupal.t('Sunday'), Drupal.t('Monday'), Drupal.t('Tuesday'), Drupal.t('Wednesday'), Drupal.t('Thursday'), Drupal.t('Friday'), Drupal.t('Saturday')];
  
  jQuery(block).find('#events-wrapper').go_paginator({
      pageSize: 6,
      feeds: keyed_feeds(Drupal.user_channels.subscriptions(1)),
      service: 'user_channels.feed_news_items', 
      filters: {},
      fields: ['nid', 'title', 'type', 'comment_count', 'created', 'field_time']
    });
    
    jQuery(block).find('#events-wrapper').bind('construct_items', function(evt, data)
      {
      var items = data.items, 
        list = $(this).find('.listing.animation'),
        zp = function(n) {
          return n<10?'0'+n:n;
        };
      
      for(var el, startTime, endTime, item, i = 0; i < items.length; i++)
      {
        item = items[i];
        startTime = new Date(item[5][0].value * 1000);
        endTime = new Date(item[5][0].value2 * 1000);
        el = list.find('li:eq(' + i + ')');
        if(!el.find('.comment-link').length)
        {
          el.append('<a class="comment-link"><em></em></a>');
        }
        if(!el.find('.node-type').length)
        {
          el.find('.permalink').before('<span class="node-type-wrapper"><img class="node-type" width="20" height="20" /></span>');
        }
        if(!el.find('.date-time').length)
        {
          el.append('<div class="date-time"><div class="date"></div><div class="time"></div></div>');
        }
        
        el.find('.node-type').attr('src', Drupal.settings.user_map.favicon_path +'/'+ item[6] +'/icon/dark.png');
        el.find('.comment-link').attr('href', Drupal.settings.basePath + 'js-node-link/'+ item[0] +'#comments').find('em').text(item[3]);
        el.find('.date').text(weekdays[startTime.getDay()].substring(0, 3) + ' ' + startTime.getDate() + ' ' + Drupal.t(months[startTime.getMonth()]).substring(0, 3));
        el.find('.time').text(startTime.getHours() + ':' + zp(startTime.getMinutes()));
      }
    });
};

jQuery(document).ready(function(){
  var block = jQuery('#block-user_channels-1').get(0);
  if (block) {
    new UserEvents(block);
  }
});
